package fsmsim.parserXML;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

/**
 * <p>Title: FSMSim</p>
 *
 * <p>Description: Simulatore di macchine a stati finiti.</p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: Bollati, Donati, Gabrielli, Peli</p>
 *
 * @author Bollati, Donati, Gabrielli, Peli
 * @version 3.0
 */

public class ValidateXML {
    
    private String fileXMLSchema;
    
    public ValidateXML(String fileXMLSchema) {
        this.fileXMLSchema = fileXMLSchema;
    }
    
    public void setFileXMLSchema(String fileXMLSchema) {
        this.fileXMLSchema = new String(fileXMLSchema);
    }
    
    public String getFileXMLSchema() {
        return this.fileXMLSchema;
    }
    
    /**
     * Valida il fileXML
     * 
     * @throws java.io.IOException
     * @throws org.xml.sax.SAXException
     */
    public boolean validateXML(String fileXML) {
        try {

            SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

            File schemaLocation = new File(fileXMLSchema);
            Schema schema = factory.newSchema(schemaLocation);

            Validator validator = schema.newValidator();

            Source source = new StreamSource(new java.io.File(fileXML));

            validator.validate(source);
            

            
        } catch (SAXException ex) {
            Logger.getLogger(ValidateXML.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (IOException ex) {
            Logger.getLogger(ValidateXML.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
        
        return true;
    }

}
